<?php
class Recherche {

	private $cle;
	private $table;
	private $autoComplete;
	
	public function __construct($map, $table) {
		$this->cle = isset($map['cle']) ? $map['cle'] : "";
		$this->table = $table;
		$this->autoComplete = false;
	}
	  
	public function getCle() { return $this->cle; }
  
	public function getTable() { return $this->table; }
	
	public function setAutoComplete($v){
		$this->autoComplete = $v;
	}
  


  	public function makeTabClause($clauseSuite="") {
		$equal=array();
		$like=array();
		$cle="";
		
		if (isset($this->cle)) {
			$cle=addslashes($this->cle);
		}

		return $this->fabriqueClause($equal,$like,$cle,$clauseSuite);
		
	}
  
	protected function fabriqueClause($tabEqual,$tabLike,$cle, $clauseSuite="") {
		
		
		$clause = "";
		if (!is_array($tabEqual)) {$tabEqual=array();}
		if (!is_array($tabLike)) {$tabLike=array();}
		

		
		foreach($tabEqual as $k=>$v) {
			if(trim($v) != ""){
				$clause.=" {$k}='{$v}' AND";
			}
		}
		
		foreach($tabLike as $k=>$v) {
			if (trim($v) != "") {
				$clause.=" {$k} LIKE '%{$v}%' AND";
			}
		}

		if (trim($cle) != "") {
			$mots = explode(' ', $cle); 
			$nb_mots = count($mots); 
			$clause .= " (";
			
			if ($this->table == "Title"){
			/*	for($i = 0; $i < $nb_mots; $i++) {
					$mot = $mots[$i];
					$clause.=" LOWER(Title.name) LIKE LOWER('{$mot}%') OR LOWER(Title.artist) LIKE LOWER('{$mot}%') AND";
				}
				$clause = substr($clause,0,-4);*/
				$motFullText = "";
				for($i = 0; $i < $nb_mots; $i++) {
					$mot = $mots[$i];
					$motFullText .=" +{$mot}";
				}
				
				if ($this->autoComplete)
					$motFullText.="*";

				$clause.=" MATCH(name,artist) AGAINST('{$motFullText}' IN BOOLEAN MODE)";
			} else {
				if ($this->table == "User"){
					for($i = 0; $i < $nb_mots; $i++) {
						$mot = $mots[$i];
						$clause.=" LOWER(User.name) LIKE LOWER('%{$mot}%') AND";
					}
				}
				$clause = substr($clause,0,-4);
			}

									
			
			$clause .= ")";
		} else {
			$clause=substr($clause,0,-4);
		}
		
		// Si rien, substr renvoit faux donc faut mettre 1=1
		
		if (! $clause) {
			$clause="1=1 ". $clauseSuite;
		} else {
			$clause .= " ". $clauseSuite;
		}

		return $clause;
	}
	
	public static function searchForm($id = "",$submitValue = "Rechercher",$idPage=""){
		$html = <<<EOT
			 <form action="index.php?a=rechercher" method="get">
                    <div class="rech">
                        <input type="text"  id="{$id}" name="cle" value="" />
                    </div>
                    <div class="submitrech"><input type="submit" id="{$idPage}" value="{$submitValue}" /></div>
                    <div class="clearer"></div>
                </form>
EOT;
		return $html;
		
	}
	
	public static function FilterUserForm($id = ""){
		$html = <<<EOT
			 <form action="index.php?a=getCat&amp;cat=utilisateurs" method="get">
                    <div class="rech">
                        <input type="text"  id="{$id}" name="cle" value="" />
                    </div>
                    <div class="submitrech"><input type="submit" value="Filtrer" /></div>
                    <div class="clearer"></div>
                </form>
EOT;
		return $html;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/*
	*
	*
	 A RETIRER UNE FOIS TERMINE
	 *
	 *
	 */
	
	public function resultatRecherche(){
		$html = "";
		$urlParamTab = $this->extraireQuery();
		$res = "";
		foreach ($this as $k=>$v){
			if ($k == "categorie" && $v !="" && $v !="TOU"){
				$url = $this->ParamsToString($urlParamTab, array("cat","a"));
				$n = $GLOBALS['categorie'][$v];
				$res .= " dans la catégorie <strong>" .$n . "</strong>" . "<a class=\"croixresultat\" href=\"index.php?a=rechercher" . $url . "\"></a> ";
			}
			if ($k == "cle" && $v !=""){
				$url = $this->ParamsToString($urlParamTab, array("cle","a"));
				$res .= "pour le mot clé <strong>" . $v . "</strong>" ."<a class=\"croixresultat\" href=\"index.php?a=rechercher" . $url . "\"></a> ";
			}	
		}
		if ($res != ""){
			$html .= "<p>Résultat " . $res ."</p>";
		}
		
		return $html;
	}
	
	public function ParamsToString($tabParam,$keyArraySoustract){
		$rub = "";
		foreach ($tabParam as $k=>$v) {
			if (! in_array($k,$keyArraySoustract)){
				$rub .= "&amp;{$k}={$v}";
			}
		}
		return $rub;
	}
	
	public function rechercheFormMini($actionUrl,$invite) {
		$cle = $this->cle;
		$urlParamTab = $this->extraireQuery();
		$rub="";
		foreach ($urlParamTab as $k=>$v) {
			if ( $k !== 'a' && $k!== 'cle' ) {
				$rub .= "<input type=\"hidden\" name=\"{$k}\" value=\"{$v}\" />";
			}
		}
		$html = <<<EOT
		
                <form action="{$actionUrl}" method="get">
					<div class="rech">
							<label for="cle">Rechercher des photos</label>
							<input type="hidden" name="a" value="rechercher"/>
							<input type="text" id="cle" name="cle" value="{$cle}" />
							{$rub}
					</div>
					<div class="submitrech"><input type="submit" value="{$invite}" /></div>
					<div class="clearer"></div>
				</form>
		
EOT;
	return $html;
		
	}
	
	public function rechercheFiltre(){
		$urlParamTab = $this->extraireQuery();
		$urlpopnew = $this->ParamsToString($urlParamTab, array("cla","a"));
		$urlanc = $this->ParamsToString($urlParamTab, array("anc","a"));
		$popnew = "";
		foreach ( $GLOBALS['classement'] as $k=>$v){
			if ($this->classement != $k){
				$popnew .= "<a href=\"index.php?a=rechercher&amp;cla={$k}{$urlpopnew}\">{$v}</a>";
			} else {
				$popnew .= "<span>{$v}</span>";
				$actpopnew = "<span>{$v}</span>";
			}
		}
		$anc = "";
		foreach ($GLOBALS['anciennete'] as $k=>$v){
			if ($this->	anciennete != $k){
				$anc .= "<a href=\"index.php?a=rechercher&amp;anc={$k}{$urlanc}\">{$v}</a>";
			} else {
				$anc .= "<span>{$v}</span>";
				$actanc = "<span>{$v}</span>";
			}
		}
		$html = <<<EOT
		
		<div id="filtrerech">
			<div id="popnew">
				{$actpopnew}
				<div class="switcher">
				{$popnew}
				</div>
			</div>
			<div id="ancien">
				{$actanc}
				<div class="switcher">
				{$anc}
				</div>
			</div>
			<div class="clearer"></div>
		</div>
EOT;
		
		return $html;	
		
	}
	
	public function extraireQuery() {
		$query = $_SERVER['QUERY_STRING'];
		$tabParams = explode('&', $query);
	
		$urlParamTab = array();
		foreach ($tabParams as $param) {
		  $tab = explode('=', $param);
		  // si param existe et != ""
		  if (isset($tab[1]) && $tab[1] !=""){
		  	$urlParamTab[$tab[0]] = $tab[1];
		  }
		}

		return $urlParamTab;
	  }
	
	public function categorieMenu($tags=false){
		$rub = "";
		$urlParamTab = $this->extraireQuery();
		foreach($GLOBALS['categorie'] as $k=>$v){
			if ($k !=="SEL"){
				$rub .= "<li><a href=\"index.php?a=rechercher&amp;cat={$k}";
				foreach ($urlParamTab as $k2=>$v2) {
					if ( $k2 !== 'a' && $k2!== 'cat' ) {
						$rub .= "&amp;{$k2}={$v2}";
					}
				}
				if ($k == $this->categorie){
					$rub .= "\" style=\"background-color:#eaeaea;\"><span>&gt; </span>{$v}</a></li>";
				} else {
					$rub .= "\"><span>&gt; </span>{$v}</a></li>";
				}
			}
		}
		
		$tagCloud = "";
		if ($tags){
			$nuageTags = NuageDeTag_Bd::lire(Outils_Bd::getInstance()->getConnexion());
			$nuageTags_Ui = new NuageDeTag_Ui($nuageTags);
			$tagCloud = "<div class=\"tagsColG\">" . $nuageTags_Ui->makeHTML(10) . "</div>";
		}
		
		$html = <<<EOT
		<div id="sidebar">
		<h2>Catégories</h2>
		<ul id="verticalMenu">
			{$rub}
		</ul>
		{$tagCloud}
		</div>
		
EOT;
		return $html;
		
	}
	/*
	public function rechercheForm($actionUrl,$invite) {
		$cle = $this->cle;
		$ville = $this->ville;
		$lieu = $this->nomLieu;
		$htmlSelect=Outils_Form::makeCombo("categorie",$GLOBALS['categories'],$this->categorie);
		$optville = ($ville != "") ? "<li>Ville : {$ville}</li>" : "";
		$optlieu = ($lieu != "") ? "<li>Lieu : {$lieu}</li>" : "";
		$optcategorie = ($this->categorie != "" && $this->categorie != "SEL") ? "<li>Categorie : {$GLOBALS['categories'][$this->categorie]}</li>" : "";
		$optfiltrage = (!($optville == "" && $optlieu == "" && $optcategorie == "")) ? "<ul>{$optcategorie}{$optville}{$optlieu}</ul>" : "<p>Aucun filtre actif</p>" ;
		
		$html = <<<EOT
		

		<div class="colgauch">
			<form style="626px;float:left;" action="{$actionUrl}" method="get">
				<input type="hidden" name="a" value="recherche" />
				<div class="blocfct">
					<div class="rech">
						<h4>Recherche par mots clés</h4>
						<div>
							<span><label for="cle">Mots clés</label></span>
							<span><input type="text" id="cle" name="cle" value="{$cle}" /></span>
						</div>
					</div>
				</div>
				<div class="blocfct">
					<div class="filt">
						<h4>Options de filtrage</h4>
						<div>
							<span><label for="categorie">Catégorie</label></span>
							<span>{$htmlSelect}</span>
						</div>
						<div class="inputfloat">
							<label for="villese">Ville</label>
							<input type="text" id="villese" name="ville" value="{$ville}" />
							<label for="lieu">Lieu</label>
							<input type="text" id="lieu" name="nomLieu" value="{$lieu}" />
						</div>
					</div>
				</div>
		</div>
		<div class="coldroite">
			<div>
				<div class="submitrech"><input type="submit" value="{$invite}" /></div>
				<h3>Options de filtrage actifs :</h3>
				{$optfiltrage}
			</div>
		</div>
		</form>
		<div class="clearer"></div>
		<script type="text/javascript">
			autoComplete();
		</script>

	
	
EOT;
		return $html;
		
	}
	*/
	
}


?>
